package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountClassifyDAO;
import com.ecoolex.discount.sales.entity.DiscountBrandClassify;
import com.ecoolex.discount.sales.entity.DiscountClassify;
import com.ecoolex.discount.sales.entity.DiscountClassify.Column;
import com.ecoolex.discount.sales.entity.DiscountClassifyExample;
import com.ecoolex.discount.sales.entity.DiscountClassifyExample.Criteria;
import com.ecoolex.discount.sales.enums.DiscountClassifyStatus;
import com.ecoolex.discount.sales.pojo.QueryDiscountClassifyDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscountClassifyManager {
	
    @Autowired
    private DiscountClassifyDAO dao;
    
    @Autowired
    private DiscountBrandClassifyManager discountBrandClassifyManager;
    
    public void verify(DiscountClassify record) {
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getName()),ResultCode.VERIFY.build(10), "分类名称为空");
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getImage()),ResultCode.VERIFY.build(11), "图片为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getRegionType()),ResultCode.VERIFY.build(12), "区域类型为空");
    	BizAssert.isTrue(Check.notNull(record.getWeight()),ResultCode.VERIFY.build(13), "权重为空");
    }
    
    public DiscountClassify addDiscountClassify(DiscountClassify record, String createUser){
    	verify(record);
    	record.setStatus(DiscountClassifyStatus.OFFLINE.real());
    	record.setDeleted(DiscountClassify.NOT_DELETED);
    	record.setCreateUser(createUser);
    	record.setCreateTime(Date.from(Instant.now()));
    	record.setUpdateUser(createUser);
    	record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
    }

	public DiscountClassify editDiscountClassify(DiscountClassify record, String updateUser){
		verify(record);
		record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.excludes(Column.regionType,Column.status,Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

	public void deleteDiscountClassify(DiscountClassify record, String updateUser){
		//检查是否存在关联的品牌
		List<DiscountBrandClassify> list = discountBrandClassifyManager.getByClassifyId(record.getClassifyId());
		BizAssert.isTrue(Check.isNullOrEmpty(list),ResultCode.VERIFY.build(10), "存在关联的折扣品牌");
		record.setDeleted(DiscountClassify.IS_DELETED);
		record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
	}

	public void onlineDiscountClassify(DiscountClassify record, String updateUser){
		record.setStatus(DiscountClassifyStatus.ONLINE.real());
		record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.status,Column.updateUser,Column.updateTime);
	}

	public void offlineDiscountClassify(DiscountClassify record, String updateUser){
		record.setStatus(DiscountClassifyStatus.OFFLINE.real());
		record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.status,Column.updateUser,Column.updateTime);
	}

	public DiscountClassify getDiscountClassify(Integer classifyId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(classifyId, DiscountClassify.NOT_DELETED);
	}

	public Pagination<DiscountClassify> queryDiscountClassify(QueryDiscountClassifyDTO query,Sorting[] sorts, Page page) {
		DiscountClassifyExample example = new DiscountClassifyExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNullOrEmpty(query.getName())) {
			criteria.andNameLike('%'+query.getName()+'%');
		}
		if(Check.notNullAndGtZero(query.getRegionType())) {
			criteria.andRegionTypeEqualTo(query.getRegionType());
		}
		criteria.andDeletedEqualTo(DiscountClassify.NOT_DELETED);
		
		example.setRows(page.getPageSize());
		example.setOffset(page.getFirstIndex());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public DiscountClassify getDiscountClassifyByBrandIdForH5(Integer brandId, Integer regionType) {
		return dao.get(brandId, regionType,DiscountClassifyStatus.ONLINE.real());
	}
}